package com.margo.project.web.mvcconfig.controller;

import java.util.List;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.margo.project.web.mvcconfig.model.MargoBodyAdviceVO;
import com.margo.project.web.mvcconfig.model.MargoConverterVO;
import com.margo.project.web.mvcconfig.model.MargoMvcConfigVO;
import com.margo.project.web.mvcconfig.requestbodyadvice.ParamJsonDeserializer;
import com.margo.project.web.mvcconfig.viewresolver.MargoReturnType;
import com.margo.project.web.utils.ResultUtils;
import com.margo.project.web.utils.ResultVO;

import io.swagger.annotations.ApiOperation;

@Controller
@RequestMapping("/mvcconfig")
public class MargoMvcConfigController {
	
	private Logger logger=LoggerFactory.getLogger(this.getClass());
	
	@ApiOperation(value="发送", notes="发送解析文本", produces="application/x-www-form-urlencoded")
	@PostMapping("/form")
	@ResponseBody
	public ResultVO queryForm(@Valid MargoMvcConfigVO margoMvcConfigVO, BindingResult result) {
		logger.info(margoMvcConfigVO.toString());
		if (result.hasErrors()) {
			List<ObjectError> lstResult = result.getAllErrors();
			StringBuffer sb=new StringBuffer();
			for (ObjectError objectError : lstResult) {
				sb.append(objectError.getDefaultMessage());
			}
			return ResultUtils.fail(sb.toString());
		}
		return ResultUtils.success(1);
	}
	
	@ApiOperation(value="发送", notes="发送解析文本")
	@PostMapping("/json")
	@ResponseBody
	public ResultVO queryJson(@RequestBody @Valid MargoMvcConfigVO margoMvcConfigVO, BindingResult result) {
		logger.info(margoMvcConfigVO.toString());
		if (result.hasErrors()) {
			List<ObjectError> lstResult = result.getAllErrors();
			StringBuffer sb=new StringBuffer();
			for (ObjectError objectError : lstResult) {
				sb.append(objectError.getDefaultMessage());
			}
			return ResultUtils.fail(sb.toString());
		}
		return ResultUtils.success(1);
	}
	
	@ApiOperation(value="发送", notes="发送解析文本")
	@PostMapping("/returntype")
	@MargoReturnType
	public ResultVO returntype(String age) {
		return ResultUtils.success(1);
	}
	
	@ApiOperation(value="发送", notes="发送解析文本")
	@PostMapping("/error")
	@ResponseBody
	public ResultVO error(@RequestBody @ParamJsonDeserializer(fieldName="workStatus",length=3) MargoBodyAdviceVO margoBodyAdviceVO) {
		logger.info(margoBodyAdviceVO.toString());
		throw new RuntimeException();
	}
	
	@ApiOperation(value="发送", notes="发送解析文本")
	@PostMapping("/jsonconverter")
	@ResponseBody
	public ResultVO jsonconverter(@RequestBody MargoConverterVO margoConverterVO) {
		logger.info(margoConverterVO.toString());
		return ResultUtils.success(1);
	}
	
	@ApiOperation(value="发送", notes="发送解析文本")
	@PostMapping("/formconverter")
	@ResponseBody
	public ResultVO formconverter(MargoConverterVO margoConverterVO) {
		logger.info(margoConverterVO.toString());
		return ResultUtils.success(1);
	}
}
